<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Testing dojo.io.iframe</title>
		<style type="text/css">
			@import "../../resources/dojo.css";
		</style>
		<script type="text/javascript" src="../../dojo.js" data-dojo-config="isDebug:true, ioPublish: true"></script>
		<script type="text/javascript">
			require(["dojo", "doh", "dojo/topic", "dojo/io/iframe"], function(dojo, doh, topic){
				doh.register([
					function ioIframeGetText(t){
						var d = new doh.Deferred();
						var td = dojo.io.iframe.send({
							url: "../request/iframeDummyMethod.php?type=text",
							method: "GET",
							timeoutSeconds: 5,
							preventCache: true,
							handle: function(res, ioArgs){
								if(!(res instanceof Error) &&
									t.is("iframe succeeded", res)){
									d.callback(true);
								}else{
									d.errback(false);
								}
							}
						});
						return d;
					},

					function ioIframeGetFormJson(t){
						var d = new doh.Deferred();
						var td = dojo.io.iframe.send({
							url: "../request/iframeDummyMethod.php",
							form: "contentArrayTest",
							content: {
								type: "json",
								color: "blue",
								size: 42
							},
							timeoutSeconds: 5,
							preventCache: true,
							handleAs: "json",
							handle: function(res, ioArgs){
								if(!(res instanceof Error) &&
									t.is("blue", res.query.color)){
									d.callback(true);
								}else{
									d.errback(false);
								}
							}
						});
						return d;
					},

					function ioIframeGetJson(t){
						var d = new doh.Deferred();
						var td = dojo.io.iframe.send({
							url: "../request/iframeDummyMethod.php?type=json",
							content: {
								color: "blue",
								size: 42
							},
							timeoutSeconds: 5,
							preventCache: true,
							handleAs: "json",
							handle: function(res, ioArgs){
								if(!(res instanceof Error) &&
									t.is("blue", res.query.color)){
									d.callback(true);
								}else{
									d.errback(false);
								}
							}
						});
						return d;
					},

					function ioIframeNotPostJson(t){
						var d = new doh.Deferred();
						var td = dojo.io.iframe.send({
							method: "post",
							url: "../request/iframeDummyMethod.php?type=json",
							content: {
								color: "blue",
								size: 42
							},
							timeoutSeconds: 5,
							preventCache: true,
							handleAs: "json",
							handle: function(res, ioArgs){
								if(!(res instanceof Error) &&
									t.is("blue", res.query.color)){
									d.callback(true);
								}else{
									d.errback(false);
								}
							}
						});
						return d;
					},

					function ioIframePostFormJson(t){
						var d = new doh.Deferred();
						var form = dojo.byId("contentArrayTest");
						form.getAttributeNode("method").value = "post";
						var td = dojo.io.iframe.send({
							method: "post",
							url: "../request/iframeDummyMethod.php?type=json",
							form: "contentArrayTest",
							content: {
								color: "blue",
								size: 42
							},
							timeoutSeconds: 5,
							preventCache: true,
							handleAs: "json",
							handle: function(res, ioArgs){
								if(!(res instanceof Error) &&
									t.is("blue", res.post.color)){
									d.callback(true);
								}else{
									d.errback(false);
								}
							}
						});
						return d;
					},

					function ioIframeGetJavascript(t){
						var d = new doh.Deferred();
						var form = dojo.byId("contentArrayTest");
						form.getAttributeNode("method").value = "get";
						var td = dojo.io.iframe.send({
							url: "iframeResponse.js.html",
							method: "GET",
							timeoutSeconds: 5,
							preventCache: true,
							handleAs: "javascript",
							handle: function(res, ioArgs){
								console.log("RES: ", res);
								if(!(res instanceof Error) &&
									t.is(42, window.iframeTestingFunction())){
									d.callback(true);
								}else{
									d.errback(false);
								}
							}
						});
						return d;
					},

					function ioIframeGetHtml(t){
						var d = new doh.Deferred();
						var td = dojo.io.iframe.send({
							url: "iframeResponse.html",
							method: "GET",
							timeoutSeconds: 5,
							preventCache: true,
							handleAs: "html",
							handle: function(res, ioArgs){
								if(!(res instanceof Error) &&
									t.is("SUCCESSFUL HTML response", res.getElementsByTagName("h1")[0].innerHTML)){
									d.callback(true);
								}else{
									d.errback(false);
								}
							}
						});
						return d;
					},

					function ioIframeGetXml(t){
						var d = new doh.Deferred();
						var td = dojo.io.iframe.send({
							url: "iframeResponse.xml",
							method: "GET",
							timeoutSeconds: 5,
							preventCache: true,
							handleAs: "xml",
							handle: function(res, ioArgs){
								if(!(res instanceof Error)
									&& t.is(4, res.documentElement.getElementsByTagName("child").length)
								){
									d.callback(true);
								} else {
									d.errback(false);
								}
							}
						});
						return d;
					},
					function ioIframeContentArray(t){
						//Tests if an array passed in content causes as an error on cleanup.
						var d = new doh.Deferred();
						var td = dojo.io.iframe.send({
							url: "iframeResponse.text.html",
							form: "contentArrayTest",
							content: {"tag": ["value1","value2"]},
							handle: d.getTestErrback(function(res, ioArgs){
								t.f(res instanceof Error);
							})
						});
						var stop = topic.subscribe("/dojo/io/stop", function(){
							stop.remove();
							d.callback(true);
						});
						return d;
					},
					function ioPublish(t){
						var d = new doh.Deferred();

						var topicCount = 0;
						topic.subscribe("/dojo/io/start", function(){
							topicCount++;
						});
						topic.subscribe("/dojo/io/send", function(){
							topicCount++;
						});
						topic.subscribe("/dojo/io/load", function(){
							topicCount++;
						});
						topic.subscribe("/dojo/io/error", function(){
							topicCount--;
						});
						topic.subscribe("/dojo/io/done", function(){
							topicCount++;
						});
						topic.subscribe("/dojo/io/stop", d.getTestCallback(function(){
							topicCount++;
							t.is(5, topicCount);
						}));

						dojo.io.iframe.send({
							url: "iframeResponse.text.html",
							method: "GET",
							timeoutSeconds: 5,
							preventCache: true
						});
						return d;
					}
				]);
				doh.runOnLoad();
/*
				// for watching in the debugger...
				dojo.addOnLoad(function(){
					var td = dojo.io.iframe.get({
						url: "iframeResponse.text.html",
						timeoutSeconds: 5,
						preventCache: true,
						handle: function(res, ioArgs){
							if(!(res instanceof Error) &&
								"iframe succeeded" == res){
								console.debug("OK");
							}else{
								console.debug("Error", res);
							}
						}
					});
				});
*/
			});

		</script>
	</head>
	<body>
		<form id="contentArrayTest" method="get" enctype="multipart/form-data">
		</form>
	</body>
</html>
